package com.hzit.auth.controller;

import com.hzit.auth.service.ITokenService;
import com.hzit.auth.service.IUserService;
import com.hzit.auth.vo.UserVo;
import com.hzit.entity.User;
import com.hzit.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wf
 * @since 2024-03-05
 */
@RestController
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private ITokenService tokenService;
	//1. 用户列表
	@GetMapping("user/list")
	public R list(){
		return R.ok(userService.list());
	}
	//2. 用户登录
	@PostMapping("/user/login")
	public R login(@RequestBody UserVo vo){
		//2.1 根据用户名查询用户是否存在
		User user = userService.findByUserVo(vo);
		//2.2 生成token并返回给前端
		Map<String,Object> result = tokenService.createToken(user);
		//2.3 返回
		return R.ok(result);
	}
}
